package com.igame.Controller;

import com.igame.Service.PrivateMessagesService;
import com.igame.Service.UserService;
import com.igame.common.Result;
import com.igame.dto.ContactDto;

import com.igame.dto.MessageDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.Set;


@CrossOrigin //忽视跨域问题
@RestController
@RequestMapping("/message")
public class PrivateMessagesController {

    @Autowired
    PrivateMessagesService privateMessagesService;

    @Autowired
    UserService userService;

    @PostMapping("/contactTheSeller")
    public Result getUpperId (@RequestBody ContactDto contactDto){
     return privateMessagesService.initiateContact(contactDto);
    }

    @PostMapping("/getAllContacts")
    public Result<Set<String>> getAllContacts(@RequestBody Integer userId){
        return privateMessagesService.getAllContacts(userId);
    }

    @PostMapping("/sendMessage")
    public Result sendMessage(@RequestBody MessageDto messageDto){
       return privateMessagesService.sendMessage(messageDto);
    }


    @PostMapping("/getMessage")
    public Result getMessage(@RequestBody MessageDto messageDto){

        return privateMessagesService.getMessagesBetweenUsers(messageDto.getSendUser(),messageDto.getReceiver());
    }

    @PostMapping("/getReceiverId")
    public Integer getReceiverId(@RequestBody String receiverName){
       return privateMessagesService.getReceiverId(receiverName);
    }

    @PostMapping("/getUnRead")
    public Result<Integer> getUnRead(@RequestBody Integer loginId){
        return privateMessagesService.getReadMessagesCountForReceiver(loginId);
    }

    @PostMapping("/updateRead")
    public void updateRead(@RequestBody MessageDto messageDto){
      privateMessagesService.updateMessageStatusForReceiver(messageDto.getSendUser(),messageDto.getReceiver());
    }


    @PostMapping("/deleteContact")
    public void deleteContact(@RequestBody MessageDto messageDto){
        privateMessagesService.deleteContact(messageDto.getSendUser(),messageDto.getReceiver());
    }
}
